<?php
namespace Home\Controller;
use Think\Controller;

class BlockManageController extends CommonController {

	//获取首页信息
	public function ajaxGetIndexInfo() {
		$banner_list = array();
		$banner_body_list = array();
		$commodity_list = array();
		$cart_list = array();
		//轮播图
		$blockManage = D('BlockManage');
		$db_res = $blockManage->where("block_id = 1 and type = 1 and is_active = 1")->order("sort desc")->select();
		if(!empty($db_res)){
			foreach($db_res as $db_one){
				$tmp['img_url'] = C('HTTP_IMG') . $db_one['img_url'];
				$tmp['href_url'] = $db_one['href_url'];
				$banner_list[] = $tmp;
			}
		}
		
		$db_res = $blockManage->where("block_id = 2 and type = 1 and is_active = 1")->order("sort desc")->select();
		if(!empty($db_res)){
			foreach($db_res as $db_one){
				$tmp['img_url'] = C('HTTP_IMG') . $db_one['img_url'];
				$tmp['href_url'] = $db_one['href_url'];
				$banner_body_list[] = $tmp;
			}
		}
		
		$commodityLimit = D('CommodityLimit');
		$limitArr = $commodityLimit->getLimitArr();
		//商品列表
		$db_res = $blockManage->where("block_id = 2 and type = 3 and is_active = 1")->find();
		if(!empty($db_res) && !empty($db_res['context'])){
			$context_arr = explode(",",$db_res['context']);
			$commoditySrv = D('CommodityInfo');
			$commoditySrv->ini();
			$commodity_tmp = $commoditySrv->commodity_info();
			foreach($commodity_tmp as $commodity){
				if($commodity['is_onsale'] && in_array($commodity['commodity_id'],$context_arr)){
					$commodity['unit_name'] = C('GOODS_UNIT_NAME.'.$commodity['unit_id']);
					$commodity['recommend'] = 1;
					if(isset($limitArr[$commodity['commodity_id']])){
						$commodity['left_num'] = num_f($limitArr[$commodity['commodity_id']]);
						$commodity['is_limit'] = true;
					}else{
						$commodity['left_num'] = 0;
						$commodity['is_limit'] = false;
					}
					$commodity_list[] = $commodity;
				}
			}
		}
		
		
		
		//购物车信息
		if($this->checkLogin()==true){
			$user_id = session('user_id');
			$cartTmp = D('CartTmp');
			$result = $cartTmp->getCartList($user_id);
			$cart_list = $result['data'];
		}
		$response['data']['banner_body_list'] = $banner_body_list;
		$response['data']['banner_list'] = $banner_list;
		$response['data']['commodity_list'] = $commodity_list;
		$response['data']['cart_list'] = $cart_list;
		$response['msg'] = '获取成功';
		$response['rsc'] = C('RESPONSE.SUCCESS');
		$this->ajaxReturn($response);
	}
	
	// 按照分类获取商品列表
	/*
	public function ajaxGetIndexCommodityByCateId() {
		$commodity_list = array();
		$r_list = array();
		$c_list = array();
		$cart_list = array();
		$category_id = num_f(I('category_id',null));
		//推鲜列表
		if(empty($category_id)){
			$response['msg'] = '参数异常';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$blockManage = D('BlockManage');
		$db_res = $blockManage->where("block_id = 2 and type = 3 and is_active = 1")->find();
		$recommend_arr = array();
		if(!empty($db_res) && !empty($db_res['context'])){
			$recommend_arr = explode(",",$db_res['context']);
		}
		$commodityCategory = D('CommodityCategory');
		$db_res = $commodityCategory->where("category_id = '".$category_id."'")->select();
		if(empty($db_res)){
			$response['msg'] = '该分类下暂无商品';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$cate_commodity_list = array();
		foreach($db_res as $tmp){
			$cate_commodity_list[] = $tmp['commodity_id'];
		}
		$commoditySrv = D('CommodityInfo');
		$commoditySrv->ini();
		$all = $commoditySrv->commodity_info();
		
		$commodityLimit = D('CommodityLimit');
		$limitArr = $commodityLimit->getLimitArr();
		
		foreach($all as $commodity){
			if($commodity['is_onsale'] && in_array($commodity['commodity_id'],$cate_commodity_list)){
				$commodity['unit_name'] = C('GOODS_UNIT_NAME.'.$commodity['unit_id']);
				
				if(!empty($limitArr[$commodity['commodity_id']])){
					$commodity['left_num'] = num_f($limitArr[$commodity['commodity_id']]);
					$commodity['is_limit'] = true;
				}else{
					$commodity['left_num'] = 0;
					$commodity['is_limit'] = false;
				}
				
				if(in_array($commodity['commodity_id'],$recommend_arr)){
					$commodity['recommend'] = 1;
					$r_list[] = $commodity;
				}else{
					$commodity['recommend'] = 0;
					$c_list[] = $commodity;
				}
			}
		}
		$commodity_list = array_merge($r_list,$c_list);
		
		//购物车信息
		if($this->checkLogin()==true){
			$user_id = session('user_id');
			$cartTmp = D('CartTmp');
			$result = $cartTmp->getCartList($user_id);
			$cart_list = $result['data'];
		}
		$response['data']['commodity_list'] = $commodity_list;
		$response['data']['cart_list'] = $cart_list;
		$response['msg'] = '获取成功';
		$response['rsc'] = C('RESPONSE.SUCCESS');
		$this->ajaxReturn($response);
	}*/
	
	// 推鲜模块
	public function ajaxGetTuiXianInfo(){
		$bm_id = I('bm_id');
		$bm_id = num_f($bm_id);
		$blockManage = D('BlockManage');
		$db_res = $blockManage->where("id = '".$bm_id."' and block_id = 4 and type = 4 and is_active = 1")->find();
		if(empty($db_res)){
			$response['data'] = null;
			$response['msg'] = '获取失败';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$imgs = array();
		if(!empty($db_res['context'])){
			$tmp_arr = explode("|",$db_res['context']);
			foreach($tmp_arr as $tmp_one){
				$imgs[] = C('HTTP_IMG') . $tmp_one;
			}
		}
		$response['data']['imgs'] = $imgs;
		$response['data']['href'] = $db_res['href_url'];
		$response['data']['title'] = $db_res['desc'];
		$response['msg'] = '获取成功';
		$response['rsc'] = C('RESPONSE.SUCCESS');
		$this->ajaxReturn($response);
		
	}
	
};